package pojo.wizards;

import javax.sql.DataSource;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import com.alibaba.druid.pool.DruidDataSource;

import pojo.model.DsModel;
import pojo.utils.FileUtils;
import pojo.utils.I18n;
import pojo.utils.UiUtils;

public class DatabaseWizardPage extends WizardPage {
	
	private Text driver;
	private Text dbName;
	private Text url;
	private Text userName;
	private Text password;
	private DsModel oldDsModel;

	protected DatabaseWizardPage(String pageName) {
		super(pageName);
		setTitle("数据库连接");
	}

	@Override
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new FillLayout(SWT.HORIZONTAL));

		SashForm sashForm = new SashForm(container, SWT.VERTICAL);
		Composite composite_2 = new Composite(sashForm, SWT.BORDER);
		GridLayout gl_composite_2 = new GridLayout(3, false);
		gl_composite_2.verticalSpacing = 10;
		composite_2.setLayout(gl_composite_2);
		driver = UiUtils.createText(composite_2, I18n.databaseInfoUI_driverLocationLabel);
		url = UiUtils.createText(composite_2, I18n.databaseInfoUI_jdbcUrlLabel);
		userName = UiUtils.createText(composite_2, I18n.databaseInfoUI_usernameLabel);
		password = UiUtils.createText(composite_2, I18n.databaseInfoUI_passwordLabel);
		dbName = UiUtils.createText(composite_2, I18n.databaseInfoUI_databaseNameLabel);
		setDefaultValue();
		setControl(container);
	}

	public DataSource getDataSource() {
		DsModel d = (DsModel) FileUtils.readFile(FileUtils.getDataSourceFileName());
		DruidDataSource ds = new DruidDataSource();
		ds.setUrl(d.getUrl());
		ds.setUsername(d.getUser());
		ds.setPassword(d.getPassword());
		ds.setDriverClassName(d.getDriver());
		return ds;
	}

	public DsModel updateDsModel() {
		DsModel dsModel = new DsModel();
		dsModel.setDriver(driver.getText());
		dsModel.setUser(userName.getText());
		dsModel.setPassword(password.getText());
		dsModel.setUrl(url.getText());
		dsModel.setDbName(dbName.getText());
		FileUtils.createFile(dsModel, FileUtils.getDataSourceFileName());
		return dsModel;
	}

	public DsModel getDsModel() {
		DsModel dsModel = new DsModel();
		dsModel.setDriver(driver.getText());
		dsModel.setUser(userName.getText());
		dsModel.setPassword(password.getText());
		dsModel.setUrl(url.getText());
		dsModel.setDbName(dbName.getText());
		return dsModel;
	}

	private void setDefaultValue() {
		oldDsModel = (DsModel) FileUtils.readFile(FileUtils.getDataSourceFileName());
		if (oldDsModel != null) {
			driver.setText(oldDsModel.getDriver());
			url.setText(oldDsModel.getUrl());
			userName.setText(oldDsModel.getUser());
			password.setText(oldDsModel.getPassword());
			dbName.setText(oldDsModel.getDbName());
		}
	}

	public Text getDriver() {
		return driver;
	}

	public Text getUrl() {
		return url;
	}

	public Text getUserName() {
		return userName;
	}

	public Text getPassword() {
		return password;
	}

}
